/******************************************************************************************/
/* Replication file for                                                                   */
/* Ching-Ching Chang, Alex Min-wei Lin & Chung-Li Wu                                      */
/* Strategic Voting Revisited: The Case of 2018 Taipei City Mayoral Election              */
/******************************************************************************************/

*clear all
* Open Stata data file named "taipei strategic voting.dta"

set scheme s2color, perm
ssc install estout, replace
ssc install distplot, replace

label define District2c 1 "北投區" 2 "士林區" 3 "內湖區" 4 "南港區" 5 "松山區" 6 "信義區" 7 "中山區" 8 "大同區" 9 "中正區" 10 "萬華區" 11 "大安區" 12 "文山區"
label define District2e 1 "Beitou" 2 "Shilin" 3 "Neihu" 4 "Nangang" 5 "Songshan" 6 "Xinyi" 7 "Zhongshan" 8 "Datong" 9 "Zhongzheng" 10 "Wanhua" 11 "Da'an" 12 "Wenshan"
label values District2 District2e
label variable BeganCountOn1730 "Polling places that began counting votes on 1730 and after"
label variable CountTime2 "Time vote-counting began"
label variable Turnout2018 "Voter turnout at polling place"
label variable Ko2018vs "Vote share for Ko Wen-je"
label variable Ting2018vs "Vote share for Ting Shou-chung"
label variable Yao2018vs "Vote share for Yao Wen-chih"
label variable n_20to39 "Number of persons aged 20 to 39"
label variable n_40to59 "Number of persons aged 40 to 59"
label variable n_over60 "Number of persons aged over 60"
label variable p_20to39 "Percent of persons aged 20 to 39"
label variable p_40to59 "Percent of persons aged 40 to 59"
label variable p_over60 "Percent of persons aged over 60"
egen party=group(DW Swing KK)
label variable party "Polling place characteristic"
label define party 1 "Safe KMT" 2 "Swing" 3 "Safe DPP"
label values party party

*Table 1 (*t-tests)
ttest Turnout2018, by(BeganCountOn1730)
ttest Ko2018vs, by(BeganCountOn1730)
ttest Ting2018vs, by(BeganCountOn1730)
ttest Yao2018vs, by(BeganCountOn1730)

*Table 2
eststo clear
eststo: mixed Ko2018vs c.CountTime2##ib2.party Turnout2018 Electorates2018 Lien2014vs p_20to39 p_college || District2:, nolog
estat icc
eststo: mixed Ko2018vs c.CountTime2##ib2.party Turnout2018 Electorates2018 Lien2014vs p_20to39 p_college if BeganCountOn1730==0 || District2:, nolog
estat icc
eststo: mixed Ko2018vs c.CountTime2##ib2.party Turnout2018 Electorates2018 Lien2014vs p_20to39 p_college if BeganCountOn1730==1 || District2:, nolog
estat icc
esttab, se aic bic scalars(N ll df_m chi2)

*Table 3
eststo clear
eststo: mixed Ting2018vs c.CountTime2##ib2.party Turnout2018 Electorates2018 Lien2014vs p_20to39 p_college || District2:, nolog
estat icc
eststo: mixed Ting2018vs c.CountTime2##ib2.party Turnout2018 Electorates2018 Lien2014vs p_20to39 p_college if BeganCountOn1730==0 || District2:, nolog
estat icc
eststo: mixed Ting2018vs c.CountTime2##ib2.party Turnout2018 Electorates2018 Lien2014vs p_20to39 p_college if BeganCountOn1730==1 || District2:, nolog
estat icc
esttab, se aic bic scalars(N ll df_m chi2)

* Table 4
eststo clear
eststo: mixed Yao2018vs c.CountTime2##ib2.party Turnout2018 Electorates2018 Lien2014vs p_20to39 p_college|| District2:, nolog
estat icc
eststo: mixed Yao2018vs c.CountTime2##ib2.party Turnout2018 Electorates2018 Lien2014vs p_20to39 p_college if BeganCountOn1730==0 || District2:, nolog
estat icc
eststo: mixed Yao2018vs c.CountTime2##ib2.party Turnout2018 Electorates2018 Lien2014vs p_20to39 p_college if BeganCountOn1730==1 || District2:, nolog
estat icc
esttab, se aic bic scalars(N ll df_m chi2)

*Figure 1
hist CountTime2, freq addlabel w(25) lcolor(black) bfcolor(gs10) yla(, ang(h)) ytitle ("Number of polling places") ///
  xla(1600 "4pm" 1700 "5pm" 1800 "6pm" 1900 "7pm" 2000 "8pm", grid) xline(1750, lc(black)) ///
  xmtic(1650 1750 1850 1950 2050) 
  
*Figure 2
distplot CountTime2, by(District2) freq xla(1600 "4pm" 1700 "5pm" 1800 "6pm" 1900 "7pm" 2000 "8pm", grid) ///
  subtitle(, size(vlarge)) yla(, ang(h)) ytitle ("Number of polling places") xmtic(1650 1750 1850 1950 2050)

*Figure 3
twoway (lowess Turnout2018 CountTime2, lp(longdash) clcolor(red)) || scatter Turnout2018 CountTime2, msize(vsmall) mcolor(black) msymbol(D) ///
  mfcolor(yellow) xlabel(1600 "4pm" 1700 "5pm" 1800 "6pm" 1900 "7pm" 2000 "8pm", labsize(small) grid) xline(1750, lc(black)) xmtic(1650 1750 1850 1950 2050) ///
  ytitle(Voter turnout) ylabel(0(.1)0.8, labsize(small) ang(h)) ///
  legend(ring(0) pos(5) order(1 "Lowess fit (observed)" 2 "Voter turnout") cols(1)) saving(turnout1, replace) 

*Figure 4
twoway (lowess Ko2018vs CountTime2, lp(longdash) clcolor(red)) || scatter Ko2018vs CountTime2, msize(vsmall) mcolor(black) ///
  mfcolor(white) xlabel(1600 "4pm" 1700 "5pm" 1800 "6pm" 1900 "7pm" 2000 "8pm", labsize(small) grid) xline(1750, lc(black)) xmtic(1650 1750 1850 1950 2050) ///
  ytitle(Vote share) ylabel(0(.1)0.6, labsize(small) ang(h)) ///
  legend(ring(0) pos(5) order(1 "Lowess fit (observed)" 2 "Vote share for Ko Wen-je") cols(1)) saving(ko1, replace) 

*Figure 5
twoway (lowess Ting2018vs CountTime2, lp(longdash) clcolor(red)) || scatter Ting2018vs CountTime2, msize(vsmall) mcolor(blue) ///
  mfcolor(white) xlabel(1600 "4pm" 1700 "5pm" 1800 "6pm" 1900 "7pm" 2000 "8pm", labsize(small) grid) xline(1750, lc(black)) xmtic(1650 1750 1850 1950 2050) ///
  ytitle(Vote share) ylabel(0(.1)0.7, labsize(small) ang(h)) ///
  legend(ring(0) pos(5) order(1 "Lowess fit (observed)" 2 "Vote share for Ting Shou-chung") cols(1)) saving(ting1, replace) 

*Figure 6
twoway (lowess Yao2018vs CountTime2, lp(longdash) clcolor(red)) || scatter Yao2018vs CountTime2, msize(vsmall) mcolor(black) ///
  mfcolor(mint) xlabel(1600 "4pm" 1700 "5pm" 1800 "6pm" 1900 "7pm" 2000 "8pm", labsize(small) grid) xline(1750, lc(black)) xmtic(1650 1750 1850 1950 2050) ///
  ytitle(Vote share) ylabel(0(.1)0.5, labsize(small) ang(h)) ///
  legend(ring(0) pos(1) order(1 "Lowess fit (observed)" 2  "Vote share for Yao Wen-chih") cols(1)) saving(yao1, replace)  

*Appendix
sum Ko2018vs Ting2018vs Yao2018vs CountTime2 party Turnout2018 Electorates2018 Lien2014vs p_20to39 p_college
